package com.liu.user.service.impl;

import com.liu.user.dao.UserDao;
import com.liu.user.entity.User;
import com.liu.user.exception.UserLoginException;
import com.liu.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.omg.CORBA.UserException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.transaction.Transactional;
import java.util.Date;

@Service
@Transactional
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;




     public  void  addUser(User user ){

         user.setId(idWorker.nextId()+"");
         user.setCreatTime(new Date());
         user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
         userDao.save(user);


     }

     public User login(String userName,String password){
         User user =userDao.findByName(userName);
         if(user==null){
             throw new UserLoginException("您输入的用户名不存在",100010);
         }
         if(bCryptPasswordEncoder.matches(password,user.getPassword())){

             return  user;
         }else {

           throw  new UserLoginException("您输入的用户名或者密码不正确",100020);
         }


     }



}
